package com.shanjiduo.newsline;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    private EditText name;
    private EditText pwd;
    private int count=60;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==2){
                handler.removeCallbacksAndMessages(null);
                sure.setText("验证成功");
                return;
            }

            sure.setText(count+"s后重新获取");
            count--;

            if (count==0){
                count=60;
                handler.removeCallbacksAndMessages(null);
                sure.setText("重新获取验证码");
                return;
            }
            handler.sendEmptyMessageDelayed(1,100);
        }
    };
    private Button sure;
    private EditText check;

    private Button enter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        name = (EditText)findViewById(R.id.user_name);
        pwd = (EditText) findViewById(R.id.user_pwd);
        check = (EditText) findViewById(R.id.check_num);
        enter = (Button)findViewById(R.id.ensure);
        sure = (Button)findViewById(R.id.bu_check);
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


               String num= check.getText().toString().trim();
                if (TextUtils.isEmpty(num)){
                    handler.sendEmptyMessage(0);
                }
                if (num.length()==6){
                    handler.sendEmptyMessage(2);
                    enter.setEnabled(true);
                }
            }
        });
        enter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username= name.getText().toString().trim();
                String userPwd= pwd.getText().toString().trim();
                if (TextUtils.isEmpty(username)){
                    Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (userPwd.length()<6||TextUtils.isEmpty(userPwd)){
                    Toast.makeText(LoginActivity.this, "密码不能小于6位", Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
